var express = require('express');
var router = express.Router();
var mongo = require("./db")
var mongodb = require("mongodb");
var ObjectId = mongodb.ObjectId;
/* GET home page. */
router.get('/', function (req, res, next) {
  // res.render('index', { title: 'Express' });
});
// 添加接口
router.post("/add", function (req, res) {
  var obj = {
    pname: req.body.pname,
    sel: req.body.sel,
    price: req.body.price,
    inventory: 0,
    date: new Date().getTime()
  }
  mongo("insert", "test09", obj, function () {
    res.send({ success: "添加成功" })
  })
})
// 查询接口
router.post("/all", function (req, res) {
  var obj = {

  };
  if (req.body.sel && req.body.sel != "全部") { //如果前端传来分类  将分类作为查询条件
    obj.sel = req.body.sel
  }

  mongo("find", "test09", obj, function (data) {
    res.send(data)
  })
})
// 删除接口
router.post("/del", function (req, res) {
  mongo("del", "test09", { _id: ObjectId(req.body.id) }, function () {
    res.send({ success: "删除成功" })
  })
})
// 库存的修改
router.post("/upInventory", function (req, res) {
  mongo("update", "test09", [{ _id: ObjectId(req.body.id) }, { inventory: req.body.inventory }], function () {
    res.send({ success: "修改成功" })
  })
})


module.exports = router;
